package com.petRescueCenter.demo.posting.controller;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.petRescueCenter.demo.common.web.VueResponse;
import com.petRescueCenter.demo.common.web.VuejsPageRequest;
import com.petRescueCenter.demo.posting.entity.Reply;
import com.petRescueCenter.demo.posting.entity.dto.ReplyDTO;
import com.petRescueCenter.demo.posting.entity.dto.ReplyQueryDTO;
import com.petRescueCenter.demo.posting.service.IPostingService;
import com.petRescueCenter.demo.posting.service.IReplyService;
import com.petRescueCenter.demo.user.entity.User;
import com.petRescueCenter.demo.user.service.IUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(description = "评论相关的api")
@RestController
@RequestMapping("/reply")
public class ReplyController {
	@Autowired
	private IReplyService replyService;
	@Autowired
	private IUserService userService;
	@Autowired
	private IPostingService postingService;

	@ApiOperation(value = "查询评论的信息", notes = "分页返回,通过评论id查找改评论信息，通过帖子id查找该帖子下的第一层评论，通过第一层回复的id查询这一层中的所有评论；通过被评论的用户id查找评论这个用户的评论的评论信息，用于提醒回复")
	@GetMapping
	public Page<ReplyDTO> getAll(ReplyQueryDTO replyQueryDTO, VuejsPageRequest pageRequest) {
		return replyService.findAll(ReplyQueryDTO.getWhereClause(replyQueryDTO), pageRequest.getPageable());

	}

	@ApiOperation(value = "添加评论", notes = "这里只需要返回评论者id，内容，被评论的评论id，帖子第一层评论id，被评论的帖子id")
	@PostMapping
	public VueResponse create(ReplyDTO replyDTO, @RequestParam("postingId") Long postingId, HttpSession session) {

		User userSession = (User) session.getAttribute("userSession");
		try {
			User user = userService.findById(userSession.getId());
			Reply reply = new Reply();
			reply = ReplyDTO.dtoToEntity(replyDTO, reply);
			if (replyDTO.getParentReplyId() != null) {
				Long parentPRId = replyDTO.getParentReplyId();
				reply.setParentReply(replyService.findById(parentPRId));
			}
			reply.setCreateTime(new Date());
			reply.setUser(user);
			reply.setPostingId(postingId);
			replyService.save(reply);
			return new VueResponse(true, "发布成功！");
		} catch (Exception e) {
			// TODO: handle exception
			return new VueResponse(false, "发布失败！");
		}

	}

	@ApiOperation(value = "删除评论", notes = "发表帖子的用户和发表该评论的用户都可以删除帖子。")
	@DeleteMapping
	public VueResponse delete(@RequestParam("id") Long id) {
		try {
			replyService.deleteById(id);
			return new VueResponse(true, "成功删除！");
		} catch (Exception e) {
			// TODO: handle exception
			return new VueResponse(false, "操作失败！");
		}
	}

}
